<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <title>Posts &laquo; Admin</title>
  <link rel="stylesheet" href="../assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="../assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="../assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="../assets/css/admin.css">
  <script src="../assets/vendors/nprogress/nprogress.js"></script>
</head>

<body>
  <script>NProgress.start()</script>

  <div class="main">
    <nav class="navbar">
      <button class="btn btn-default navbar-btn fa fa-bars"></button>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="profile.html"><i class="fa fa-user"></i>个人中心</a></li>
        <li><a href="login.html"><i class="fa fa-sign-out"></i>退出</a></li>
      </ul>
    </nav>
    <div class="container-fluid">
      <div class="page-title">
        <h1>所有文章</h1>
        <a href="post-add.html" class="btn btn-primary btn-xs">写文章</a>
      </div>
      <div class="page-action">
        <!-- show when multiple checked -->
        <a class="btn btn-danger btn-sm" href="javascript:;" style="display: none">批量删除</a>
        <form class="form-inline" id="screenForm">
          <select name="category" class="form-control input-sm" id="categoryBox">

          </select>
          <select name="state" class="form-control input-sm">
            <option value="">所有状态</option>
            <option value="0">草稿</option>
            <option value="1">已发布</option>
          </select>
          <button class="btn btn-default btn-sm">筛选</button>
        </form>
        <ul class="pagination pagination-sm pull-right" id="fenyeBox">

        </ul>
      </div>
      <table class="table table-striped table-bordered table-hover">
        <thead>
          <tr>
            <th>标题</th>
            <th>作者</th>
            <th>分类</th>
            <th class="text-center">发表时间</th>
            <th class="text-center">状态</th>
            <th class="text-center" width="100">操作</th>
          </tr>
        </thead>
        <tbody id="articleBox">

        </tbody>
      </table>
    </div>
  </div>

  <div class="aside">
    <div class="profile">
      <img class="avatar" src="../uploads/avatar.jpg">
      <h3 class="name">Coder</h3>
    </div>
    <ul class="nav">
      <li>
        <a href="index.html"><i class="fa fa-dashboard"></i>仪表盘</a>
      </li>
      <li class="active">
        <a href="#menu-posts" data-toggle="collapse">
          <i class="fa fa-thumb-tack"></i>文章<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-posts" class="collapse in">
          <li class="active"><a href="posts.html">所有文章</a></li>
          <li><a href="post-add.html">写文章</a></li>
          <li><a href="categories.html">分类目录</a></li>
        </ul>
      </li>
      <li>
        <a href="comments.html"><i class="fa fa-comments"></i>评论</a>
      </li>
      <li>
        <a href="users.html"><i class="fa fa-users"></i>用户</a>
      </li>
      <li>
        <a href="#menu-settings" class="collapsed" data-toggle="collapse">
          <i class="fa fa-cogs"></i>设置<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-settings" class="collapse">
          <li><a href="slides.html">图片轮播</a></li>
          <li><a href="settings.html">网站设置</a></li>
        </ul>
      </li>
    </ul>
  </div>

  <script src="../assets/vendors/jquery/jquery.js"></script>
  <script src="../assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script src="../assets/vendors/art-template/template-web.js"></script>
  <script>NProgress.done()</script>
  <!-- 定义筛选分类模板 -->
  <script type="text/html" id="screenTpl">
    <option value="">所有分类</option>
    {{each data}}
      <option value="{{$value._id}}">{{$value.title}}</option>
    {{/each}}
  </script>

  <!-- 定义文章列表展示模板 -->
  <script type="text/html" id="articleTpl">
    {{each data}}
    <tr>
      <td>{{$value.title}}</td>
      <td>{{$value.author == null ? '管理员' : $value.autho}}</td>
      <td>{{$value.category && $value.category.title}}</td>
      <td class="text-center">{{$imports.formateDate($value.createAt)}}</td>
      <td class="text-center">{{$value.state == 0 ? '草稿' : '已发布'}}</td>
      <td class="text-center">
        <a href="javascript:;" class="btn btn-default btn-xs">编辑</a>
        <a href="javascript:;" data-id="{{$value._id}}" class="btn btn-danger btn-xs delete">删除</a>
      </td>
    </tr>
    {{/each}}
  </script>

  <!-- 定义分页模板 -->
  <script type="text/html" id="fenyeTpl">
    {{if page > 1}}
    <li><a href="javascript:;" onclick="changePage({{page-1}})">上一页</a></li>
    {{/if}}

    {{each display}}
    <li><a href="javascript:;" onclick="changePage({{$value}})">{{$value}}</a></li>
    {{/each}}

    {{if page < pages}}
    <li><a href="javascript:;" onclick="changePage({{page+1}})">下一页</a></li>
    {{/if}}
  </script>

  <script>
    // $(function () {
    //文章列表展示
    $.ajax({
      type: 'get',
      url: `http://47.111.184.55:3000/posts`,
      success: function (response) {
        // console.log(response)
        var html = template('articleTpl', {
          data: response.records
        });
        $('#articleBox').html(html);
        var html = template('fenyeTpl', response);
        $('#fenyeBox').html(html);
      }
    })

    //处理日期事件格式
    function formateDate(data) {
      //将日期字符串转换为日期对象
      date = new Date(data)
      return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
    }

    template.defaults.imports.formateDate = formateDate
    //分页处理函数
    function changePage(page) {
      $.ajax({
        type: 'get',
        url: `http://47.111.184.55:3000/posts`,
        data: {
          page: page,
        },
        success: function (response) {
          // console.log(response)
          var html = template('articleTpl', {
            data: response.records
          });
          $('#articleBox').html(html);
          var html = template('fenyeTpl', response);
          $('#fenyeBox').html(html);
        }
      })
    }

    //筛选数据展示功能
    //发送ajax请求，获取所有分类信息
    $.ajax({
      type: 'get',
      url: `http://47.111.184.55:3000/categories`,
      success: function (response) {
        var html = template('screenTpl', {
          data: response
        });
        $('#categoryBox').html(html);
      }
    })

    //为筛选表单注册submit自动提交事件
    $('#screenForm').on('submit', function () {
      //获取所有筛选的条件信息
      var formData = $(this).serialize()
      //发送ajax请求，并调用模板渲染数据
      $.ajax({
        type: 'get',
        url: `http://47.111.184.55:3000/posts`,
        data: formData,
        success: function (response) {
          var html = template('articleTpl', {
            data: response.records
          });
          $('#articleBox').html(html);
          var html = template('fenyeTpl', response);
          $('#fenyeBox').html(html);
        }
      })
      //阻止表单默认提交行为
      return false;
    })

    //删除文章
    //利用事件委托为删除按钮注册点击事件
    $('#articleBox').on('click','.delete',function(){
      //获取当前选中文章的id
      var id = $(this).attr('data-id')
      //询问是否真的删除当前文章
      var isDelete = confirm('确定要删除吗?');
      if(isDelete){
        //发送ajax请求
        $.ajax({
          type:'delete',
          url: `http://47.111.184.55:3000/posts/${id}`,
          success:function(response){
            location.reload()
          }
        })
      }
    });
    // })
  </script>
</body>

</html>